<?php
Globals::requireClass('Database');

class DatabaseSqlite extends Database
{
	protected function error()
	{
		$errorInfo	= $this->link->errorInfo();
		$errorCode	= $errorInfo[1];
		$errorString= $errorInfo[2];
		
		Globals::error('Database error ['.$errorCode.']: '.$errorString);
	}
	
	public function connect($server = null, $username = null, $password = null)
	{
	}
	
	/******************** config setters ********************/
	protected function setDatabase($value)
	{
		if (!empty($value))
		{
			try { $this->link = new PDO('sqlite:'.$value); }
			catch (PDOException $e) { $this->error(); }
		}
	}
	
	protected function setCharset($value)
	{
	}
	
	/******************** query methods ********************/
	public function escape($string)
	{
		return str_replace("'", "''", $string);
	}
	
	public function query($query)
	{
		$this->queryString = $query;
		$this->result = $this->link->query($query);
		
		if (!($this->result instanceof PDOStatement))
			return $this->error();
		
		return $this->result;
	}
	
	public function seek($result = null, $offset = 0)
	{
	}
	
	public function fetch($result = null, $numMode = false)
	{
		if (!$result)
			$result = $this->result;
		
		try { return $result->fetch($numMode ? PDO::FETCH_NUM : PDO::FETCH_ASSOC); }
		catch (PDOException $e) { $this->error(); }
	}
	
	public function getInsertId()
	{
		try { return $this->link->lastInsertId(); }
		catch (PDOException $e) { $this->error(); }
	}
	
	public function getAffectedRows()
	{
		try { return $this->result->rowCount(); }
		catch (PDOException $e) { $this->error(); }
	}
}
